package me.zhengjie.modules.web.service.mybatisplus.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.domain.GroupTest;
import me.zhengjie.modules.system.domain.GroupTestDetail;
import me.zhengjie.modules.system.service.mapstruct.GroupTestExpMapper;
import me.zhengjie.modules.web.domain.test.BatchTestDetail;
import me.zhengjie.modules.web.service.mybatisplus.IGroupTestDetailPlusService;
import me.zhengjie.modules.web.service.mybatisplus.IGroupTestPlusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

@Service
public class GroupTestPlusServiceImpl extends ServiceImpl<GroupTestExpMapper, GroupTest> implements IGroupTestPlusService {

    @Autowired
    private IGroupTestDetailPlusService groupTestDetailPlusService;
    @Override
    public BigDecimal getReportPriceByGroupCode(String groupCode) {

        LambdaQueryWrapper<GroupTestDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupTestDetail::getSerialCode,groupCode);
        GroupTestDetail one = groupTestDetailPlusService.getOne(queryWrapper);
        if(Objects.isNull( one)){
            return BigDecimal.ZERO;
        }
        GroupTest groupTest = getById(one.getGroupId());
        if(Objects.nonNull(groupTest)){
            return new BigDecimal(groupTest.getReportPrice());
        }
        return BigDecimal.ZERO;
    }

    @Override
    public BatchTestDetail getBatchDetailByCode(String groupCode) {
        BatchTestDetail batchTestDetail = new BatchTestDetail();
        batchTestDetail.setGroupIndex(0);
        batchTestDetail.setReportPrice(BigDecimal.ZERO);
        LambdaQueryWrapper<GroupTestDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupTestDetail::getSerialCode,groupCode);
        GroupTestDetail one = groupTestDetailPlusService.getOne(queryWrapper);
        if(Objects.isNull(one)){
            return batchTestDetail;
        }
        batchTestDetail.setGroupIndex(one.getSerialNumber());
        GroupTest groupTest = getById(one.getGroupId());
        if(Objects.isNull(groupTest)){
            return batchTestDetail;
        }
        batchTestDetail.setReportPrice(new BigDecimal(groupTest.getReportPrice()));
        return batchTestDetail;
    }

    @Override
    public BigDecimal getReportPriceByGroupId(String groupId) {

        GroupTest groupTest = getById(groupId);
        if(Objects.nonNull(groupTest)){
            return new BigDecimal(groupTest.getReportPrice());
        }
        return BigDecimal.ZERO;
    }
}
